package com.molichuxing.services.infrastructure.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.molichuxing.services.business.dto.request.modify.StoreDeviceBizModifyDto;
import com.molichuxing.services.infrastructure.bean.StoreDeviceBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.molichuxing.services.infrastructure.dao.StoreDeviceMapper;
import com.molichuxing.services.infrastructure.dto.response.StoreDeviceDto;
import com.molichuxing.services.infrastructure.service.StoreDeviceService;

/**
 * <p>
 * 门店安装的设备表 服务实现类
 * </p>
 *
 * @author zph
 * @since 2019-08-12
 */
@Service("storeDeviceService")
public class StoreDeviceServiceImpl implements StoreDeviceService {


    @Autowired
    StoreDeviceMapper storeDeviceMapper;

    /**
     * 根据门店id获取设备id信息
     *
     * @param storeId
     * @return
     */
    @Override
    public List<Integer> getDeviceIdByStoreId(Integer storeId) throws Exception {
        return storeDeviceMapper.getListByStoreId(storeId);
    }


    /**
     * 查询设备是否和其它门店绑定
     *
     * @param storeId
     * @param deviceIdList
     * @return
     * @throws Exception
     */
    @Override
    public List<Integer> getStoreDeviceByDeviceIds(Integer storeId, List<Integer> deviceIdList) throws Exception {
        return storeDeviceMapper.getCountByIds(storeId, deviceIdList);
    }

    /**
     * 批量添加门店设备
     *
     * @param storeDeviceDtoList
     * @return
     * @throws Exception
     */
    @Override
    public Boolean createStoreDeviceBatch(List<StoreDeviceDto> storeDeviceDtoList) throws Exception {
        return (storeDeviceMapper.createStoreDeviceBatch(storeDeviceDtoList) == storeDeviceDtoList.size()) ? true : false;
    }

    /**
     * 删除门店设备
     *
     * @param ids
     * @return
     * @throws Exception
     */
    @Override
    public Boolean deleteStoreDeviceByDeviceId(Integer storeId, List<Integer> ids) throws Exception {
        return (storeDeviceMapper.deleteBatchIds(storeId, ids) == ids.size()) ? true : false;
    }

    /**
     * 根据门店id删除门店设备
     *
     * @param storeId
     * @return
     * @throws Exception
     */
    @Override
    public Boolean deleteStoreDeviceByStoreId(Integer storeId) throws Exception {
        storeDeviceMapper.deleteByStoreId(storeId);
        return true;
    }

    /**
     * 获取门店id
     *
     * @param deviceId 设备id
     * @return
     */
    @Override
    public Integer getStoreIdByDevice(Integer deviceId) {
        List<Integer> storeIds = storeDeviceMapper.getListByDevice(deviceId);
        if (storeIds == null || storeIds.isEmpty()) {
            return null;
        }
        return storeIds.get(0);
    }

    /**
     * 获取门店id
     *
     * @param deviceIdListh
     * @return
     */
    @Override
    public List<StoreDeviceDto> getStoreIdsByDevice(List<Integer> deviceIdListh) {
        List<StoreDeviceBean> storeIdsByDevice = storeDeviceMapper.getStoreIdsByDevice(deviceIdListh);
        return Convert.toStoreDeviceDtoList(storeIdsByDevice);
    }

    @Override
    public Boolean modifyStoreDevice(StoreDeviceBizModifyDto storeDeviceBizModifyDto) throws Exception {
        Integer deviceId = storeDeviceBizModifyDto.getDeviceId();
        StoreDeviceBean storeDeviceBean = Convert.toStoreDeviceBean(storeDeviceBizModifyDto);
        List<Integer> listByDevice = storeDeviceMapper.getListByDevice(deviceId);
        if (listByDevice != null && listByDevice.size() > 0) {
            int result = storeDeviceMapper.modifyStoreDevice(storeDeviceBean);
            if (result > 0) {
                return true;
            } else {
                return false;
            }
        } else {
            int result = storeDeviceMapper.create(storeDeviceBean);
            if (result > 0) {
                return true;
            } else {
                return false;
            }
        }
    }

    @Override
    public List<Integer> getDeviceIdByStoreIds(List<Integer> storeIdList) {
        if (null == storeIdList || storeIdList.size() < 1) {
            return null;
        }
        return storeDeviceMapper.getDeviceIdByStoreIds(storeIdList);
    }

}
